/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InOutMonitorPane.java
 *
 * Created on 15.3.2011, 15:07:55
 */
package cz.muni.fi.rum.gui.component;

import cz.muni.fi.rum.sender.Factory;
import cz.muni.fi.rum.sender.command.RapMessage;
import cz.muni.fi.rum.sender.listener.SenderEvent;
import cz.muni.fi.rum.sender.listener.SenderListener;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import javax.swing.JToolBar;
import org.jdesktop.application.Action;
import org.jdesktop.application.Task;

/**
 *
 * @author pmikulasek
 */
public class InOutMonitorPane extends javax.swing.JPanel {

    private static final String NEW_LINE = "\n";
    private static final String SEPARATOR = "= = = = = = = = = = = = = = = = = = = =";
    private static final String EMPTY_STRING = "";
    private static InOutMonitorPane instance;
    private AtomicLong size;

    /** Creates new form InOutMonitorPane */
    private InOutMonitorPane() {
        size = new AtomicLong();
        initComponents();
        createAndRegistrateSenderListener();
    }

    public static InOutMonitorPane getInstance() {
        if (instance == null) {
            instance = new InOutMonitorPane();
        }
        return instance;
    }

    public void addValue(String text) {
        this.inOutText.append(text);
    }

    public void addNewLine() {
        this.inOutText.append(NEW_LINE);
    }

    public void addSeparator() {
        this.inOutText.append(SEPARATOR);
    }

    public void addMessage(String message) {
        addNewLine();
        addSeparator();
        addNewLine();
        this.inOutText.append(message);
        addNewLine();
    }

    public void addRapMessage(RapMessage message) {
        addSeparator();
        addNewLine();
        this.inOutText.append(message.getValue());
    }

    public void clear() {
        this.inOutText.setText(EMPTY_STRING);
    }

    private void createAndRegistrateSenderListener() {
        Factory.getSenderManager().registerSenderListener(new SenderListener() {

            @Override
            public void actionPerformed(SenderEvent e) {
                if (SenderEvent.Type.send.equals(e.getEventType())) {
                    List<RapMessage> historyLog = Factory.getSenderManager().getHistoryLog();
                    for (int i = size.intValue(); i < historyLog.size(); i++) {
                        addRapMessage(historyLog.get(i));
                    }
                    size.set(historyLog.size());
                }
            }
        });
    }

    @Action
    public Task sendAction() {
        return Factory.getSenderManager().sendAllRequest();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        inOutText = new javax.swing.JTextArea();
        inOutToolBar = new javax.swing.JToolBar();
        sendButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        inOutText.setColumns(20);
        inOutText.setEditable(false);
        inOutText.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        inOutText.setRows(5);
        inOutText.setName("inOutText"); // NOI18N
        jScrollPane1.setViewportView(inOutText);

        inOutToolBar.setOrientation(JToolBar.VERTICAL);
        inOutToolBar.setBorderPainted(false);
        inOutToolBar.setName("inOutToolBar"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getActionMap(InOutMonitorPane.class, this);
        sendButton.setAction(actionMap.get("sendAction")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getResourceMap(InOutMonitorPane.class);
        sendButton.setText(resourceMap.getString("sendButton.text")); // NOI18N
        sendButton.setFocusable(false);
        sendButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        sendButton.setName("sendButton"); // NOI18N
        sendButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        inOutToolBar.add(sendButton);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        inOutToolBar.add(jButton1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(inOutToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(inOutToolBar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea inOutText;
    private javax.swing.JToolBar inOutToolBar;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton sendButton;
    // End of variables declaration//GEN-END:variables
}
